// 此文件用于处理公募基金固定逻辑，对部分代码进行分离,让主文件只涉及核心逻辑
import store from '@/store'
import { jsonArray } from '@/api/productApi/file'
import { Message } from 'element-ui'
import Vue from 'vue'

// 判断步骤是否已经走过
export function stepIsSuccess(res, _this) {
	if (res.data.body.recordFundAddInfo.baseMode) {
		_this.active = 1
	}
	if (res.data.body.recordFundAddInfo.dateMode) {
		_this.active = 2
	}
	if (res.data.body.recordFundAddInfo.organMode) {
		_this.active = 3
	}
	if (res.data.body.recordFundAddInfo.rateMode) {
		_this.active = 4
	}
	if (res.data.body.recordFundAddInfo.investMode) {
		_this.active = 5
	}
	if (res.data.body.recordFundAddInfo.fundManagerMode) {
		_this.active = 6
	}
	if (!!res.data.body.recordFundAddInfo.fileMode && _this.title !== '查看') {
		_this.active = 7
	}
	if (res.data.body.recordFundAddInfo.finishMode) {
		// 完成页面节点标志
	}
	_this.isCom(_this.active)
}

// 基金代码拼基金名称
export function templateTitle(obj) {
	return `${obj.baseInfo.fundCode ? obj.baseInfo.fundCode : ''}-${obj.baseInfo.fundFullName ? obj.baseInfo.fundFullName : ''}`
}

// 非空返回值
export function notEmpty(property) {
	return property || ''
}

// 成立数据
export function returnEstablished() {
	const form = jsonArray(store.state.fundStep.pucliktTotalData)
	form.recordFundChangeInfo = form.fundCustodianInfo
	form.raisingStatus = form.fundCustodianInfo.raisingStatus
	form.fundRaiseType = '1'
	return form
}

// 变更保存数据
export function updateSaveData() {
	const form = jsonArray(store.state.fundStep.pucliktTotalData)
	form.filedCheckStatus = '0'
	form.fundRaiseType = '1'
	form.productFundApply.filedCheckStatus = '0'
	return form
}

// 变更','清算','终止','成立' && 状态 === '3'
export function submitData1(_this) { // 提交数据
	const form = jsonArray(store.state.fundStep.pucliktTotalData)
	form.recordFundChangeInfo = form.fundCustodianInfo
	form.recordFundChangeInfo.changeText = [...store.state.fundStep.pulicAttribute].toString()
	form.filedCheckStatus = '0'
	form.fundRaiseType = '1'
	form.type = _this.isEnterDateHtml ? _this.isEnterDateHtml : '0'
	return form
}

// 变更','清算','终止','成立' && 状态 === '7'
export function submitData2(_this) { // 提交数据
	const form = jsonArray(store.state.fundStep.pucliktTotalData)
	form.recordFundChangeInfo = form.fundCustodianInfo
	form.recordFundChangeInfo.changeText = [...store.state.fundStep.pulicAttribute].toString()
	form.filedCheckStatus = '0'
	form.fundRaiseType = '1'
	form.type = _this.isEnterDateHtml ? _this.isEnterDateHtml : '0'
	form.fundSysCode = _this.fundSysCode
	return form
}

// 复核 && 状态 === '4'
export function checkoutData1(_this, type) { // 复核数据
	const obj = {
		fundSysCode: _this.fundSysCode,
		fundRaiseType: '1',
		personLiableId: _this.submitInfo.personLiableId ? _this.submitInfo.personLiableId : '',
		checkLiableId: _this.submitInfo.checkLiableId ? _this.submitInfo.checkLiableId : '',
		filedCheckStatus: type === '确认无误' ? '1' : '2'
	}
	return obj
}

// 复核 && 状态 !== '4'
export function checkoutData2(_this, type) { // 复核数据
	const obj = {
		fundSysCode: _this.fundSysCode,
		personLiableId: store.state.fundStep.pucliktTotalData.fundCustodianInfo.personLiableId,
		fundRaiseType: '1',
		filedCheckStatus: type === '确认无误' ? '1' : '2'
	}
	return obj
}

// 重新申请
export function reapplyData() {
	const form = jsonArray(store.state.fundStep.pucliktTotalData)
	if (form.productFundApply.investInfo.specialTypeChar) {
		if (form.productFundApply.investInfo.specialTypeChar.includes('其他')) {
			if (!form.productFundApply.investInfo.specialTypeName) {
				Message({ type: 'warning', customClass: 'el-messageClass', message: '投资相关页面请填写 其他-特殊品种名' })
				return false
			}
		}
	}
	form.personLiableId = form.fundCustodianInfo.personLiableId
	form.checkLiableId = form.fundCustodianInfo.checkLiableId
	form.fundRaiseType = '1'
	return form
}

// 改变最后步骤标题
export function updateLastTile(_this) {
	if (_this.title === '重新申请') {
		_this.stepList.forEach(item => {
			if (item.key === 'submitInfo') {
				Vue.set(item, 'title', '重新申请-完成')
			}
		})
	}
	if (_this.title === '复核') {
		_this.stepList.forEach(item => {
			if (item.key === 'submitInfo') {
				Vue.set(item, 'title', _this.dataType === '6' ? '变更复核-完成' : '新增复核-完成')
			}
		})
	}
	if (_this.dataType === '7') {
		_this.stepList.forEach(item => {
			if (item.key === 'submitInfo') {
				Vue.set(item, 'title', '重新变更-完成')
			}
		})
	}
}
